﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DLL.PicNews
{
	/// <summary>
	/// 控制类
	/// </summary>
	/// <![CDATA[2014-8-19 14:34:04]]>
	public class Component : DLL.ComponentBase
	{
		#region 属性
		/// <summary>
		/// 问题数据类对象
		/// </summary>
		/// <![CDATA[2014-8-19 14:34:04]]>
		private Database clsDatabase;		// 数据类对象
		/// <summary>
		/// 配置类对象
		/// </summary>
		/// <![CDATA[2014-8-19 14:34:04]]>
		private Config clsConfig = new Config();		// 配置类对象
		/// <summary>
		/// 文件内容控制类
		/// </summary>
		/// <![CDATA[2014-8-19 15:36:35]]>
		private FileContent.Component clsFileContentComp;		// 文件内容控制类
		#endregion 属性

		#region 构造函数
		/// <summary>
		/// 控制类构造函数
		/// </summary>
		/// <![CDATA[2014-8-19 14:34:04]]>
		public Component(System.Web.UI.Page page)
			: base(page)
		{
			clsDatabase = new Database(page);
			clsFileContentComp = new FileContent.Component(page);

			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nID", clsDatabase.ID, clsDatabase.listStructure[clsDatabase.ID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nStatus", clsDatabase.STATUS, clsDatabase.listStructure[clsDatabase.STATUS].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strFileName", clsDatabase.FILENAME, clsDatabase.listStructure[clsDatabase.FILENAME].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strPicFileName", clsDatabase.PICFILENAME, clsDatabase.listStructure[clsDatabase.PICFILENAME].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strShowDate", clsDatabase.SHOWDATE, clsDatabase.listStructure[clsDatabase.SHOWDATE].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nUserID", clsDatabase.USERID, clsDatabase.listStructure[clsDatabase.USERID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nDepartmentID", clsDatabase.DEPARTMENTID, clsDatabase.listStructure[clsDatabase.DEPARTMENTID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strDateTime", clsDatabase.DATETIME, clsDatabase.listStructure[clsDatabase.DATETIME].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strIP", clsDatabase.IP, clsDatabase.listStructure[clsDatabase.IP].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nFileContentID", clsDatabase.FILECONTENTID, clsDatabase.listStructure[clsDatabase.FILECONTENTID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nPicContentID", clsDatabase.PICCONTENTID, clsDatabase.listStructure[clsDatabase.PICCONTENTID].strName));

			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nID", clsDatabase.listStructure[clsDatabase.ID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nStatus", clsDatabase.listStructure[clsDatabase.STATUS].strName, "="));
		}
		#endregion 构造函数

		#region 类型转换
		protected override TonyNet4V1.EntityBase GetEntity()
		{
			return new Entity();
		}
		protected override TonyNet4V1.QueryEntityBase GetQuery()
		{
			return new QueryEntity();
		}
		protected override TonyNet4V1.DataBase GetData()
		{
			return new Database(page);
		}
		/// <summary>
		/// 把查询结果链表中的实体类型转为现在的实体类型
		/// </summary>
		/// <![CDATA[2014-8-19 14:34:04]]>
		/// <param name="list"></param>
		/// <returns></returns>
		private List<Entity> ConvertListEntityType(List<TonyNet4V1.EntityBase> list)
		{
			List<Entity> listEntity = new List<Entity>();

			for (int i = 0; i < list.Count; i++)
			{
				Entity clsEntity = new Entity();

				clsEntity = (Entity)list[i];

				listEntity.Add(clsEntity);
			}

			return listEntity;
		}
		#endregion 类型转换

		#region 查询
		/// <summary>
		/// 根据ID获得实体对象
		/// </summary>
		/// <![CDATA[2014-8-19 14:34:04]]>
		/// <param name="nID"></param>
		/// <returns></returns>
		public new Entity GetOne(int nID)
		{
			return (Entity)base.GetOne(nID);
		}

		/// <summary>
		/// 查询所有
		/// </summary>
		/// <![CDATA[2014-8-19 14:34:04]]>
		/// <returns></returns>
		public new List<Entity> QueryAll()
		{
			return ConvertListEntityType(base.QueryAll());
		}

		/// <summary>
		/// 查询
		/// </summary>
		/// <![CDATA[2014-8-19 14:34:04]]>
		/// <returns></returns>
		public List<Entity> Query(QueryEntity clsQuery)
		{
			return ConvertListEntityType(base.Query(clsQuery));
		}
		#endregion 查询


		#region 添加、修改等
		/// <summary>
		/// 添加
		/// </summary>
		/// <![CDATA[2014-8-19 16:22:44]]>
		/// <param name="clsEntity"></param>
		/// <returns></returns>
		public override bool Add(TonyNet4V1.EntityBase clsEntity)
		{
			Entity clsAdd = new Entity();

			clsAdd = (Entity)clsEntity;
			clsAdd.DateTime = System.DateTime.Now.ToString();

			return base.Add(clsAdd);
		}
		/// <summary>
		/// 添加
		/// </summary>
		/// <![CDATA[2014-8-19 16:21:25]]>
		/// <param name="clsEntity"></param>
		/// <param name="fUploadPic"></param>
		/// <param name="fUploadFile"></param>
		/// <returns></returns>
		public bool Add(Entity clsEntity, System.Web.UI.WebControls.FileUpload fUploadPic, System.Web.UI.WebControls.FileUpload fUploadFile)
		{
			FileContent.Entity clsPic = new FileContent.Entity();
			FileContent.Entity clsFile = new FileContent.Entity();
			Entity clsAddEntity = new Entity();

			clsAddEntity = (Entity)clsEntity;
			clsPic = clsFileContentComp.Add(fUploadPic);
			clsFile = clsFileContentComp.Add(fUploadFile);

			if (clsPic != null)
			{
				clsAddEntity.PicFileName = clsPic.FileName;
				clsAddEntity.PicContentID = clsPic.ID;
			}
			if (clsFile != null)
			{
				clsAddEntity.FileName = clsFile.FileName;
				clsAddEntity.FileContentID = clsFile.ID;
			}

			return Add(clsAddEntity);
		}
		#endregion 添加、修改等
	}
}
